package com.hducms.cms.action.front;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hducms.cms.entity.assist.CmsComment;
import com.hducms.cms.entity.assist.CmsCommentExt;
import com.hducms.cms.entity.front.CommentListEntity;
import com.hducms.cms.entity.front.CommentM;
import com.hducms.cms.entity.main.CmsGroup;
import com.hducms.cms.entity.main.CmsUser;
import com.hducms.cms.service.CmsCommentService;
import com.hducms.cms.service.CmsUserExtService;
import com.hducms.cms.service.ContentService;
import com.hducms.cms.web.CmsUtils;
import com.hducms.common.util.ClientUtils;
import com.hducms.common.util.DateFormatUtils;
import com.hducms.common.web.RequestUtils;

@Controller
public class CommentActM {

	@RequestMapping(value="/comment.m")
	public void comment(HttpServletRequest request,
			HttpServletResponse response,Integer contentId,String text,String publickey) {
		String ip = RequestUtils.getIpAddr(request);
		CmsUser user = CmsUtils.getUser(request);
		Integer userId = user.getUserId();
		//评论是否需要审核
		CmsGroup group = user.getGroup();
		Boolean need_check =  group.getNeedCheck();
		//
		CmsComment comment = new CmsComment();
		CmsCommentExt commentExt = new CmsCommentExt();
		Date date = new Date();
		date = new Timestamp(System.currentTimeMillis());
		comment.setContentId(contentId);
		comment.setCommentUserId(userId);
		comment.setCreateTime(date);
		comment.setChecked(!need_check);
		try {
			manager.save(comment);
			commentExt.setId(comment.getId());
			commentExt.setIp(ip);
			commentExt.setText(text);
			manager.saveE(commentExt);
		} catch (Exception e) {
			ClientUtils.resultResponse(response, 0,publickey); //评论失败返回0
			return;
		}
		ClientUtils.resultResponse(response, 1,publickey);//评论成功返回1
	}
	
	/*
	 * 获取对应内容的评论
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getcomment.m")
	public void getConmmentList(Integer cid, Integer pno, HttpServletResponse response,
			HttpServletRequest request,String publickey) {
		if (null == pno) {
			pno = 1;
		}
		List<CmsComment> cmsComments = new ArrayList<CmsComment>();
		cmsComments = (List<CmsComment>) commentService.getPage(cid,
				null, false, false, true, pno,
				5).getList();
		List<CommentM> commentMs = new ArrayList<CommentM>();
		for (CmsComment cmsComment : cmsComments) {
			CommentM temp = new CommentM();
			temp.setCid(cid);
			Date date = new Date();
			date = cmsComment.getCreateTime();
			temp.setTime(DateFormatUtils.gettDateString(date));
			temp.setTxtString(cmsComment.getCommentExt().getText());
			String realName = userExtService.findById(cmsComment.getCommentUserId()).getRealname();
			if (realName==null ||realName.equals("")) {
				temp.setUsername("匿名");
			}
			else {
				temp.setUsername(realName);
			}
			commentMs.add(temp);
		}
		CommentListEntity entiy = new CommentListEntity();
		entiy.setItems(commentMs);
		if (!commentMs.isEmpty()) {
			entiy.setNextPage(pno+1);
		}
		
		ClientUtils.resultResponse(response, entiy,publickey); //获取评论返回
	}
	
	@Autowired
	private CmsCommentService manager;
	@Autowired
	private CmsUserExtService userExtService;
	@Autowired
	private CmsCommentService commentService;
	@Autowired
	private ContentService contentService;
}
